package cn.iocoder.yudao.module.engine.service.core.filter.read.deprecated;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.engine.enums.EnumAudioType;
import cn.iocoder.yudao.module.engine.model.context.ControlEngineContext;
import cn.iocoder.yudao.module.engine.model.engine.EngineOutputData;
import cn.iocoder.yudao.module.engine.service.core.pipeline.FilterAdapter;
import cn.iocoder.yudao.module.engine.service.core.pipeline.FilterContext;
import cn.iocoder.yudao.module.engine.util.AudioUtils;
import cn.iocoder.yudao.module.engine.util.EngineConvertUtils;
import cn.iocoder.yudao.module.engine.util.EngineStrUtils;
import cn.iocoder.yudao.module.infra.api.config.IConfigApi;
import cn.iocoder.yudao.module.resource.api.IAudioServiceApi;
import cn.iocoder.yudao.module.resource.api.enums.MessageSourceTypeEnum;
import cn.iocoder.yudao.module.resource.api.model.AudioDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 添加前后缀音频
 */
@Deprecated
@Component
@Slf4j
public class AttachAudioPrefixAndSuffixFilter extends FilterAdapter {
    private static final String CONFIG_KEY_PREFIX_NEED = "prefix.tag.need"; // 是否需要前缀的标签
    private static final String CONFIG_KEY_PREFIX_TAG = "prefix.tag.name"; // 前缀标签
    private static final String CONFIG_KEY_SUFFIX_NEED = "suffix.tag.need"; // 是否需要后缀的标签
    private static final String CONFIG_KEY_SUFFIX_TAG = "suffix.tag.name"; // 后缀标签

    @Resource
    private IConfigApi configApi;

    @Resource
    private IAudioServiceApi audioApi;


    @Override
    public void read(FilterContext ctx, ControlEngineContext engineContext) throws Exception {
        final List<EngineOutputData.Audio> audioList = AudioUtils.getAudioList(engineContext);
        if (CollUtil.isNotEmpty(audioList)) {
            for (EngineOutputData.Audio audio : audioList) {
                // 判断标签
                final List<String> tagNameList = audio.getTagNameList();
                if (CollUtil.isEmpty(tagNameList)) {
                    log.info("音频不包含标签, audioId = {}", audio.getAudioId());
                    continue;
                }

                attachPrefixAudio(engineContext, audio, tagNameList);
                attachSuffixAudio(engineContext, audio, tagNameList);
            }
        }

        ctx.fireRead(engineContext);
    }

    private void attachSuffixAudio(ControlEngineContext engineContext, EngineOutputData.Audio audio, List<String> tagNameList) {
        final String audioId = audio.getAudioId();
        final String tagNeedSuffix = configApi.getValueByKey(CONFIG_KEY_SUFFIX_NEED);
        final boolean containsSuffix = EngineStrUtils.containsMultiValues(tagNameList, tagNeedSuffix);
        log.info("是否包含后缀, audioId = {}, containsSuffix = {}, tagNeedSuffix = {}, tagNameList = {}",
                audioId, containsSuffix, tagNeedSuffix, tagNameList);
        if (containsSuffix) {
            final String tagSuffixName = configApi.getValueByKey(CONFIG_KEY_SUFFIX_TAG);
            final List<String> suffixTags = EngineStrUtils.getContainsMultiValues(tagNameList, tagSuffixName);
            if (CollUtil.isEmpty(suffixTags)) {
                log.warn("没有找到后缀标签, audioId = {}, tagNameList = {}, tagSuffixName = {}", audioId, tagNameList, tagSuffixName);
                return;
            }

            final List<AudioDTO> suffixAudioList = audioApi.queryAudioByTag(engineContext.getRoleId().intValue(), suffixTags);
            log.info("查询后缀音频, audioId = {}, suffixTags = {}, suffixAudioListSize = {}",
                    audioId, suffixTags, suffixAudioList.size());
            final AudioDTO suffixAudio = AudioUtils.randomAudio(engineContext.getSessionCtx(), suffixAudioList);
            log.info("随机后缀音频, audioId = {}, suffixAudio = {}", audioId, JsonUtils.toJsonString(suffixAudio));
            audio.setSuffix(EngineConvertUtils.toEngineOutputData(engineContext.getUserText(), engineContext.getChatId(), MessageSourceTypeEnum.SYSTEM, EnumAudioType.SUFFIX, suffixAudio));
        }
    }

    private void attachPrefixAudio(ControlEngineContext engineContext, EngineOutputData.Audio audio, List<String> tagNameList) {
        final String audioId = audio.getAudioId();
        final String tagNeedPrefix = configApi.getValueByKey(CONFIG_KEY_PREFIX_NEED);
        final boolean containsPrefix = EngineStrUtils.containsMultiValues(tagNameList, tagNeedPrefix);
        log.info("是否包含前缀, audioId = {}, containsPrefix = {}, tagNeedPrefix = {}, tagNameList = {}",
                audioId, containsPrefix, tagNeedPrefix, tagNameList);
        if (containsPrefix) {
            final String tagPrefixName = configApi.getValueByKey(CONFIG_KEY_PREFIX_TAG);
            final List<String> prefixTags = EngineStrUtils.getContainsMultiValues(tagNameList, tagPrefixName);
            if (CollUtil.isEmpty(prefixTags)) {
                log.warn("没有找到前缀标签, audioId = {}, tagNameList = {}, tagPrefixName = {}", audioId, tagNameList, tagPrefixName);
                return;
            }

            final List<AudioDTO> prefixAudioList = audioApi.queryAudioByTag(engineContext.getRoleId().intValue(), prefixTags);
            log.info("查询前缀音频, audioId = {}, prefixTags = {}, prefixAudioListSize = {}",
                    audioId, prefixTags, prefixAudioList.size());
            final AudioDTO prefixAudio = AudioUtils.randomAudio(engineContext.getSessionCtx(), prefixAudioList);
            log.info("随机前缀音频, audioId = {}, prefixAudio = {}", audioId, JsonUtils.toJsonString(prefixAudio));
            audio.setPrefix(EngineConvertUtils.toEngineOutputData(engineContext.getUserText(), engineContext.getChatId(), MessageSourceTypeEnum.SYSTEM, EnumAudioType.PREFIX, prefixAudio));
        }
    }


}
